package com.basksoft.baskreport.demo.dataset;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.math.RandomUtils;

import com.basksoft.report.core.model.dataset.BeanDataset;
import com.basksoft.report.core.model.dataset.impl.BeanContext;
import com.basksoft.report.core.model.dataset.impl.Field;
import com.basksoft.report.core.model.dataset.impl.FieldType;

public class TimeCollectionDataset extends BeanDataset {
	private static int day = 0;
	private static List<Map<String,Object>> datas=new ArrayList<Map<String,Object>>();
	@Override
	public List<?> getData(BeanContext context) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.YEAR, -1);
		calendar.add(Calendar.DATE, day);
		if (datas.size()==0) {
			for (int i=0; i<10; i++) {
				Map<String, Object> data = new HashMap<String, Object>();
				calendar.add(Calendar.DATE, 1);
				day++;
				if (day>365) {
					day = 0;
				}
				data.put("time", calendar.getTime());
				data.put("number", RandomUtils.nextInt(300000));
				datas.add(data);
			}
		}
		else {
			datas.remove(0);
			Map<String, Object> data = new HashMap<String, Object>();
			calendar.add(Calendar.DATE, 1);
			day++;
			if (day>365) {
				day = 0;
			}
			data.put("time", calendar.getTime());
			data.put("number", RandomUtils.nextInt(300000));
			datas.add(data);
		}
		return datas;
	}

	@Override
	public List<String> getDependCells() {
		return null;
	}

	@Override
	protected String name() {
		return "数据时间采集";
	}

	@Override
	public List<Field> getFields() {
		List<Field> fields = new ArrayList<Field>();
		fields.add(new Field("time", FieldType.Date));
		fields.add(new Field("number", FieldType.Integer));
		return fields;
	}

}
